package com.yydream.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file.uploadPath}")
    private String uploadPath;

    @Value("${file.filePattern}")
    private String filePattern;

    @Override
    public void addResourceHandlers (ResourceHandlerRegistry registry) {

//        将匹配上的/files/**虚拟路径的url映射到文件上传的服务器的目录，获取静态资源
        registry.addResourceHandler("/" + filePattern + "/**").addResourceLocations("file:" + uploadPath).setCachePeriod(0);

        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

}
